package com.example.sword;

import android.os.Looper;

public class Test55 {

    public static void main(String[] args) {
        System.out.println("haha");
    }

    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }


    }


    /**
     * 给一个链表，若其中包含环，请找出该链表的环的入口结点，否则，输出null。
     */

    public ListNode EntryNodeOfLoop(ListNode pHead) {

        ListNode slow = pHead;
        ListNode fast = pHead;

        while (fast != null && fast.next != null) {

            slow = slow.next;
            fast = fast.next.next;

            if (slow == fast) {
                fast = pHead;

                while (fast != slow) {
                    fast = fast.next;
                    slow = slow.next;
                }

                if (fast == slow){
                    return fast;
                }
            }
        }
        return null;
    }
}
